﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using AnOtherBlog.Core;

namespace AnOtherBlog.AJAX.Admin.Users
{
	/// <summary>
	/// Show all users and allow edit their properties
	/// Original from MiniCommerc (DashCommerce)
	/// </summary>
	public partial class UserList : AnOtherBlog.Core.Web.AdminBasePage
	{

		#region Page Events

		/// <summary>
		/// Handles the Load event of the Page control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected void Page_Load(object sender, EventArgs e)
		{
			// TODO: Revisar esto. No encuentro otra manera de conectar con la referencia en la clase base.
			//base.LblMsg = this.lblMsg;
			
			try
			{
				SetUserListProperties();
				if (!Page.IsPostBack)
				{
					BindMembershipUserCollection();
				}
			}
			catch (Exception ex)
			{
				MsgSetCritical(ex.Message);
			}
		}

		/// <summary>
		/// Handles the Click event of the btnSearch control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected void btnSearch_Click(object sender, EventArgs e)
		{
			try
			{
				if (!string.IsNullOrEmpty(txtSearchBy.Text.Trim()))
				{
					MembershipUserCollection membershipUserCollection = null;
					string text = txtSearchBy.Text.Trim();
					text = text.Replace("*", "%");
					text = text.Replace("?", "_");
					if (ddlSearchBy.SelectedIndex == 0 /* userID */)
					{
						membershipUserCollection = Membership.FindUsersByName(text);
					}
					else
					{
						membershipUserCollection = Membership.FindUsersByEmail(text);
					}
					gvUserList.PageSize = 1000; //set this number high so as to display all results
					gvUserList.PageIndex = 0;
					gvUserList.DataSource = membershipUserCollection;
					gvUserList.RowDataBound += new GridViewRowEventHandler(gvUserList_RowDataBound);
					gvUserList.DataBind();
					lblNumberOfTotalUsers.Text = membershipUserCollection.Count.ToString();

					hlShowAll.Visible = true;
				}
			}
			catch (Exception ex)
			{
				MsgSetCritical(ex.Message);
			}
		}

		/// <summary>
		/// Enableds the changed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		public void EnabledChanged(object sender, EventArgs e)
		{
			try
			{
				string userID = null;
				CheckBox checkBox = sender as CheckBox;
				if (checkBox == null)
					return;

				if (!string.IsNullOrEmpty(checkBox.Attributes["Value"]))
					userID = checkBox.Attributes["Value"];

				if (userID == null)
					return;

				MembershipUser user = Membership.FindUsersByName(userID)[userID];
				user.IsApproved = checkBox.Checked;

				Membership.UpdateUser(user);
			}
			catch (Exception ex)
			{
				MsgSetCritical(ex.Message);
			}
		}

		/// <summary>
		/// Handles the Click event of the lbDelete control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
		protected void lbDelete_Click(object sender, CommandEventArgs e)
		{
			try
			{
				if (e.CommandName.Equals("mydelete"))
				{
					string userName = (string)e.CommandArgument;
					Membership.DeleteUser(userName);
					Response.Redirect("~/Admin/Users/UserList.aspx", false);
				}
			}
			catch (Exception ex)
			{
				MsgSetCritical(ex.Message);
			}
		}

		/// <summary>
		/// Handles the PageIndexChanging event of the gvUserList control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
		protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			BindMembershipUserCollection();
			gvUserList.PageIndex = e.NewPageIndex;
			gvUserList.DataBind();
		}

		/// <summary>
		/// Handles the RowDataBound event of the gvUserList control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
		void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				LinkButton lbDelete = e.Row.Cells[5].FindControl("lbDelete") as LinkButton;
				if (lbDelete != null)
				{
					lbDelete.Enabled = true;
				}
			}
		}

		#endregion

		#region Protected Methods

		/// <summary>
		/// Formats the edit URL.
		/// </summary>
		/// <param name="userName">Name of the user.</param>
		/// <returns></returns>
		protected string FormatEditUrl(string userName)
		{
			return string.Format("UserEdit.aspx?userName={0}", userName);
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Sets the user list properties.
		/// </summary>
		private void SetUserListProperties()
		{
			this.Title = Resources.AdminUsers.lblUserAdministration;

			this.hlAddUser.ToolTip = Resources.AdminUsers.hlAddUser;
			this.hlShowAll.ToolTip = Resources.AdminUsers.hlShowAll;

			if (!Page.IsPostBack)
			{
				ddlSearchBy.Items.Add(new ListItem(Resources.AdminUsers.lblUserName));
				ddlSearchBy.Items.Add(new ListItem(Resources.AdminUsers.lblEmailAddress));
			}
		}

		/// <summary>
		/// Binds the membership user collection.
		/// </summary>
		private void BindMembershipUserCollection()
		{
			MembershipUserCollection membershipUserCollection = Membership.GetAllUsers();
			gvUserList.DataSource = membershipUserCollection;
			gvUserList.RowDataBound += new GridViewRowEventHandler(gvUserList_RowDataBound);

			gvUserList.DataBind();
			lblNumberOfTotalUsers.Text = membershipUserCollection.Count.ToString();
		}

		#endregion

	}
}
